<template>
  <j-modal
    :title="$t('结账单')"
    :width="width"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
  >
    <template slot="footer">
      <a-button v-has="'BisDiffCheckOutController:export'" type="primary" @click="handleExportXls($t('结账单'))">{{$t('导出')}}</a-button>
      <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
    </template>
    <div>
      <a-row :gutter="[10, 20]">
        <a-col :span="14" :offset="1">
          <div>{{$t('账期')}}：{{ model.startTime }}~{{ model.createTime }}</div>
        </a-col>
         <a-col :span="8">
          <div>{{$t('商家')}}：{{ model.supplierName }}（{{ model.supplierCode }}）</div>
        </a-col>
      </a-row>
      <a-row :gutter="[0, 30]">
        <a-col>
          <ul>
            <li style="font-weight: bold">{{$t('商品销售明细')}}</li>
          </ul>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="22" :offset="1">
          <a-table :columns="columns5" :dataSource="dataSource5" :pagination="false" bordered></a-table>
        </a-col>
      </a-row>
      <a-row :gutter="[0, 30]">
        <a-col>
          <ul>
            <li style="font-weight: bold">{{$t('扣费明细')}}</li>
          </ul>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="22" :offset="1">
          <a-table :columns="columns6" :dataSource="dataSource6" :pagination="false" bordered></a-table>
        </a-col>
      </a-row>
    </div>
  </j-modal>
</template>

<script>
import { getAction,downFile } from '@/api/manage'
export default {
  name: 'BisDiffCheckOutModal',
  components: {
  },
  data() {
    return {
      width: 800,
      visible: false,
      disableSubmit: false,
      model: {},
      columns5: [
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'amount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'cash',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        }
      ],
      columns6: [
        {
          title: this.$t('扣费项目'),
          align: 'center',
          dataIndex: 'payType_dictText',
        },
        {
          title: this.$t('业务笔数'),
          align: 'center',
          dataIndex: 'amount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('扣费值'),
          align: 'center',
          dataIndex: 'deductFee',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
      ],
      dataSource5: [],
      dataSource6: [],
    }
  },
  methods: {
    edit(record) {
      this.columns5=[
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'amount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'cash',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('结算价'),
          align: 'center',
          dataIndex: 'cost',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        }
      ]
      console.log(localStorage.getItem('operateCoin'))
      console.log(localStorage.getItem('operateLottery'))
      console.log(localStorage.getItem('operateALottery'))
      console.log(localStorage.getItem('operateACoin'))
      if (localStorage.getItem('operateCoin') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣币'),
          align: 'center',
          dataIndex: 'coin',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        })
      }
      if (localStorage.getItem('operateLottery') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣票'),
          align: 'center',
          dataIndex: 'lottery',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateALottery') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣A票'),
          align: 'center',
          dataIndex: 'lotteryA',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateACoin') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣A币'),
          align: 'center',
          dataIndex: 'coinA',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      this.dataSource5 = []
      this.dataSource6 = []
      this.visible = true
      this.model = Object.assign({}, record)
      getAction('/diffcheckout/bisDiffCheckOut/queryBisDiffCheckOutGoodsByMainId', {
        id: this.model.id
      }).then((res) => {
        if (res.success) {
          this.dataSource5 = res.result
          if (this.dataSource5.length > 0) {
            this.addSumRow(['goodsName', 'amount', 'cash', 'coin', 'lottery','coinA', 'lotteryA', 'cost'], this.dataSource5)
          }
        }
      })
      getAction('/diffcheckout/bisDiffCheckOut/queryBisDiffCheckOutDeductByMainId', {
        id: this.model.id
      }).then((res) => {
        if (res.success) {
          this.dataSource6 = res.result
        }
      })
    },
    addSumRow(myArr, dataSource) {
      let total = {}
      myArr.forEach((tcn, ind) =>{
        if (ind == 0) {
          total[tcn] = this.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      dataSource.push(total)
    },
    close() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleExportXls(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = '导出文件'
      }
      let param = {}
      param.id = this.model.id
      console.log('导出参数', param)
      downFile('/diffcheckout/bisDiffCheckOut/exportXls', param).then((data) => {
        if (!data) {
          this.$message.warning(this.$t('文件下载失败'))
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
  },
}
</script>